/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.poligran.polla.mundialista.ingesoft.dao;

import co.com.poligran.polla.mundialista.ingesoft.base.Sala;
import co.com.poligran.polla.mundialista.ingesoft.base.Usuario;
import static co.com.poligran.polla.mundialista.ingesoft.dao.Conexion.conexionSqlServer;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;

/**
 *
 * @author DAVID MOTTA
 */
public class SqlServerSalaDAO implements SalaDAO{

    @Override
    public Collection getSalas() {
        Conexion connect = null;
        LinkedList salas = null;
        Sala sala = null;
        Connection conexion = connect.conexionSqlServer();
        
        try{
            CallableStatement statement = (CallableStatement)conexion.prepareCall("EXEC sp_GetSala");
            ResultSet resultSet = statement.executeQuery();
            if(resultSet.next()){
                sala = new Sala();
                salas = new LinkedList();
                
                sala.setFechaPartido(resultSet.getDate("fechaPartido"));
                sala.setEquipoUno(resultSet.getString("equipoUno"));
                sala.setEquipoDos(resultSet.getString("equipoDos"));
                sala.setValor(resultSet.getString("Monto"));
                sala.setEstado(resultSet.getString("estado"));
                salas.add(sala);
                
                
            }
           }
        catch(Exception ex){
            System.out.print("exepction  "+ex.getMessage());
            throw new Exception(ex.getMessage());
            
            
        }
        finally{
            return salas;
        }         
    }
    public boolean insertSala(Sala sala){
        boolean inserto = false;
        Connection conexion = conexionSqlServer();
        try
        {
        CallableStatement statement = (CallableStatement)conexion.prepareCall("EXEC sp_InsertMesa ?,?,?,?,?,?");  
        
         statement.setString("idMesa", sala.getIdSala());
         statement.setDate("fechaPartido", sala.getFechaPartido());
         statement.setString("EquipoUno", sala.getEquipoUno());
         statement.setString("EquipoDos", sala.getEquipoDos());
         statement.setString("Valor", sala.getValor());
         statement.setString("idEstado", sala.getEstado());
        
        statement.executeUpdate();
        
        inserto = true;
        }
         catch(Exception ex){
            System.out.print("exepction  "+ex.getMessage());
            throw new Exception(ex.getMessage());
            
            
        }
        finally
        {
        
        return inserto;
        
    }
}
    public Collection selectSala(){
         Sala sala = null;
        Conexion conexion = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs= null;
        LinkedList selectSala = null;
        StringBuilder sqlConsulta = new StringBuilder();
        try{
            conn = conexion.conexionSqlServer();
            sqlConsulta.append("select * from sala");
            ps = conn.prepareStatement(sqlConsulta.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs=ps.executeQuery();
            selectSala = new LinkedList();
            while(rs.next()){
                sala = new Sala();
                
                sala.setIdSala(rs.getString(1));
                sala.setFechaPartido(rs.getDate(2));
                sala.setFechaPartido(rs.getDate(3));
                sala.setEquipoUno(rs.getString(4));
                sala.setEquipoDos(rs.getString(5));
                sala.setValor(rs.getString(6));
                sala.setEstado(rs.getString(7));
                
                selectSala.add(sala);
            
                
                
            }
        }
        catch( SQLException sqle) {
            sqle.printStackTrace();
            System.out.println(sqle.getMessage());
    }
        finally{
            try{
                if(rs!=null)
                {
                    rs.close();
                }
            }
             catch (SQLException sqlers) {
                sqlers.printStackTrace();
                System.out.println("Resultset:");
                System.out.println(sqlers.getMessage());
        }
            try
            {
             if (ps!=null) {
                    ps.close();
                }
            }
            catch (SQLException sqleps) {
                sqleps.printStackTrace();
                System.out.println("PreparedStatement:");
                System.out.println(sqleps.getMessage());
            }
            try {
                if (conn!=null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException sqleCon) {
                sqleCon.printStackTrace();
                System.out.println("Connection:");
                System.out.println(sqleCon.getMessage());
            }
        }
        return selectSala;
}
    public boolean updateSala (Sala sala){
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    
    
}